package com.panshi.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.panshi.entity.AppMatterCheck;
import com.panshi.mapper.MatterMapper;
import com.panshi.entity.AppAnswer;
import com.panshi.entity.AppMatter;
import com.panshi.service.FeignAnswerApiClient;
import com.panshi.service.FeignAppUserClient;
import com.panshi.service.MatterService;
import com.panshi.tools.DataResult;
import com.panshi.tools.PageBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 问题service
 */
@Service
@Slf4j
public class MatterServiceImpl implements MatterService {

    @Autowired
    private MatterMapper matterMapper;

    @Autowired // 回答服务提供端
    private FeignAnswerApiClient feignAnswerApiClient;

    @Autowired // 用户服务提供端
    private FeignAppUserClient feignAppUserClient;


    @Override
    public DataResult queryMatterHot(PageBean pageBean) {
        PageHelper.startPage(pageBean.getPageNumber(),pageBean.getPageSize());
        List<AppMatter> list = matterMapper.queryMatterHot();
        List<AppMatter> listInfo = this.setInfo(list);
        PageInfo<AppMatter> pageInfo = new PageInfo<>(listInfo);
        log.info("{}", pageInfo);
        DataResult result = this.setResult(pageInfo);
        return result;
    }

    @Override
    public DataResult queryAnswered(Integer classifyId, PageBean pageBean) {
        List<AppMatter> list = null;
        PageHelper.startPage(pageBean.getPageNumber(),pageBean.getPageSize());
        if(null == classifyId){
            list = matterMapper.queryAnswered(null);
        }else{
            list = matterMapper.queryAnswered(classifyId);
        }
        List<AppMatter> listInfo = this.setInfo(list);
        for (AppMatter m:listInfo
        ) {
            System.out.println(m.toString());
        }
        PageInfo<AppMatter> pageInfo = new PageInfo<>(listInfo);
        DataResult result = this.setResult(pageInfo);
        return result;
    }

    @Override
    public DataResult queryUnanswered(Integer classifyId, PageBean pageBean) {
        List<AppMatter> list = null;
        PageHelper.startPage(pageBean.getPageNumber(),pageBean.getPageSize());
        if(null == classifyId){
            list = matterMapper.queryUnanswered(null);
        }else{
            list = matterMapper.queryUnanswered(classifyId);
        }
        List<AppMatter> listInfo = this.setInfo(list);
        PageInfo<AppMatter> pageInfo = new PageInfo<>(listInfo);
        DataResult result = this.setResult(pageInfo);
        return result;
    }

    @Override
    public DataResult queryRecommend(Integer classifyId, PageBean pageBean) {
        List<AppMatter> list = null;
        PageHelper.startPage(pageBean.getPageNumber(),pageBean.getPageSize());
        if(null == classifyId){
            list = matterMapper.queryRecommend(null);
        }else{
            list = matterMapper.queryRecommend(classifyId);
        }
        List<AppMatter> listInfo = this.setInfo(list);
        PageInfo<AppMatter> pageInfo = new PageInfo<>(listInfo);
        DataResult result = this.setResult(pageInfo);
        return result;
    }

    @Override
    public DataResult queryMatterInfo(Integer matterId) {
        AppMatter appMatter = matterMapper.queryMatterInfo(matterId);
        List<AppMatter> list = new ArrayList<>();
        list.add(appMatter);
        List<AppMatter> listInfo = this.setInfo(list);
        DataResult result = new DataResult();
        result.setCode(200).setCount(1000).setMsg("响应成功").setData(listInfo);
        return result;
    }



    public AppMatter queryInfo(Integer matterId) {
        return matterMapper.queryMatterInfo(matterId);
    }

    @Override
    public void updateCount(Integer matterId, String code) {
        matterMapper.updateCount(matterId, code);
    }

    @Override
    public void addMatter(AppMatterCheck appMatterCheck) {
        matterMapper.addMatter(appMatterCheck);
    }

    @Override
    public void deleteMatter(Integer matterId) {
        matterMapper.deleteMatter(matterId);
    }

    private DataResult setResult(PageInfo<AppMatter> pageInfo){
        DataResult result = new DataResult();
        result.setCode(200);
        result.setCount(1000);
        result.setMsg("响应成功");
        result.setData(pageInfo.getList());

        return result;
    }

    private List<AppMatter> setInfo(List<AppMatter> list){
        List<AppAnswer> answerList;
        Integer userId = null;
        if(0 != list.size()){
            for(AppMatter m: list){
                log.info("{}",m.getMatterId());
                if(null != m.getMatterId() && 0 != m.getMatterId()){
                    // 查询matterId对应的回答
                    answerList = feignAnswerApiClient.queryAnswerByMid(m.getMatterId());
                    for(AppAnswer a: answerList){
                        if(m.getMatterId().equals(a.getMatterId())){
                            m.setAnswersList(answerList);
                        }
                    }
                }

                System.out.println("userId: "+m.getUserId());
                userId = m.getUserId();
                if(userId != null && userId != 0 && userId.equals(m.getUserId())){
                    // 查询问题发布用户，并set到AppMatter对象中
                    m.setAppUser(feignAppUserClient.queryUserInfo(userId));

                }
            }
        }

        return list;
    }

}
